// VBConversions Note: VB project level imports
using System.Data;
using DevExpress.XtraEditors.Controls;
using System.Diagnostics;
using System.Xml.Linq;
using DevExpress.XtraEditors.Repository;
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
using System.Configuration;
using System.Collections;
using DevExpress.XtraEditors;
using System.Data.Sql;
using Microsoft.SqlServer;
using System.Windows.Forms;
using System;
using DevExpress.XtraReports;
using Microsoft.SqlServer.Management.Smo;
using DevExpress;
using System.Linq;
using System.IO;
// End of VB project level imports

using System.Text;
using System.Security.Cryptography;

//	CryptographyHelper & Cryptography .NET classes
//	http://maxvergelli.wordpress.com/

//	Copyright (c) 2010 Max Vergelli

//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:

//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.

//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.


namespace SCIEF.SysBFW.CommonExt
{
	namespace Utils
	{
		public class CryptographyHelper
		{

            public EncodingBaseTypes EncodingType { get; set; }

			public enum EncodingBaseTypes
			{
				Hex = 1,
				Base64 = 2
			}

			public enum HashTypes
			{
				MD5 = 1,
				SHA1 = 2,
				SHA256 = 3,
				SHA512 = 4
			}

            public CryptographyHelper(EncodingBaseTypes encoding_type = EncodingBaseTypes.Hex)
			{
               
                ////by default encryption/hashing outputs hexadecimal strings and decryption get hexstrings as input
                //if (encoding_type != EncodingBaseTypes.Hex & encoding_type != EncodingBaseTypes.Base64)
                //{
                //    encoding_type = EncodingBaseTypes.Hex;
                //}
				EncodingType = encoding_type;
			}


            public string GetHash(string input, HashTypes hash_type)
			{
				
				Cryptography crypto = new Cryptography();
				byte[] bytes = null;
				switch (hash_type)
				{
					case HashTypes.MD5:
						bytes = crypto.CreateMD5Hash(input);
						break;
					case HashTypes.SHA1:
						bytes = crypto.CreateSHA1Hash(input);
						break;
					case HashTypes.SHA256:
						bytes = crypto.CreateSHA256Hash(input);
						break;
					case HashTypes.SHA512:
						bytes = crypto.CreateSHA512Hash(input);
						break;
					default:
						bytes = crypto.CreateMD5Hash(input);
						break;
				}
				string hash = "";
				switch (EncodingType)
				{
					case EncodingBaseTypes.Hex:
						hash = crypto.BytesToHexString(bytes);
						break;
					case EncodingBaseTypes.Base64:
						hash = crypto.BytesToBase64(bytes);
						break;
					default:
						hash = crypto.BytesToHexString(bytes);
						break;
				}
				crypto = null;
				return hash;
				
			}
			
			public string Encrypt(string input, string password)
			{
				
				Cryptography crypto = new Cryptography(password);
				byte[] bytes = crypto.Encrypt(input);
				string encrypted = "";
				if (bytes != null)
				{
					switch (EncodingType)
					{
						case EncodingBaseTypes.Hex:
							encrypted = crypto.BytesToHexString(bytes);
							break;
						case EncodingBaseTypes.Base64:
							encrypted = crypto.BytesToBase64(bytes);
							break;
						default:
							encrypted = crypto.BytesToHexString(bytes);
							break;
					}
				}
				crypto = null;
				return encrypted;
				
			}
			
			public string Decrypt(string input, string password)
			{
				
				string decrypted = "";
				if (input.Length > 0)
				{
					Cryptography crypto = new Cryptography(password);
					byte[] enc_bytes = null;
					switch (EncodingType)
					{
						case EncodingBaseTypes.Hex:
							enc_bytes = crypto.HexStringToBytes(input);
							break;
						case EncodingBaseTypes.Base64:
							enc_bytes = crypto.Base64ToBytes(input);
							break;
						default:
							enc_bytes = crypto.HexStringToBytes(input);
							break;
					}
					decrypted = crypto.Decrypt(enc_bytes);
					crypto = null;
				}
				return decrypted;
				
			}
		}
	}
}
